﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using FreeSrun.Core;

namespace FreeSrun.Forms
{
    public partial class FrmMain : Form
    {
        
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string[] args = { "-u", txtUsername.Text, "-p", txtPassword.Text, "-add", txtAddress.Text };
            if (Util.CheckParam(args))
            {
                Util.Configure(args);
                try
                {
                    DoLogin();
                }
                catch (Exception ex)
                {
                    Util.logger.AppendLog(ex);
                }
            }
            else
            {
                MessageBox.Show("运行参数错误");
            }
        }

        public void DoLogin()
        {
            LoginResponseResult result = Util.Login(Util.LOGIN_URL, Util.USERNAME, Util.PASSWORD);
            if (result.Status ==ResponseStatus.Success)
            {
                Util.UID = result.Uid;
                Util.HEARTBEAT_PACKET = Util.BuildHeartBeatPacket(Util.UID);
                Util.HeartBeat();
                Util.IS_LOGIN = true;
                // Update UI
                lblHint.Text = "已登录\n时间戳偏差为："+result.TimeOffset;
                txtAddress.Enabled = false;
                txtPassword.Enabled = false;
                txtUsername.Enabled = false;
                btnLogin.Enabled = false;

                // Hide Form
                this.WindowState = FormWindowState.Minimized;

                // Get notify message
                if (Util.NOTIFY_LAST > 0)
                {
                    string msg = Util.GetNotifyMessage();
                    ntyIcon.BalloonTipText = msg;
                    ntyIcon.ShowBalloonTip(Util.NOTIFY_LAST * 1000);
                }

            }
            else
            {
                lblHint.Text = result.Message;
            }
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            try
            {
                LogoutResponseResult result = Util.Logout(Util.LOGOUT_URL, Util.UID);
                lblHint.Text = result.Message;
                txtAddress.Enabled = true;
                txtUsername.Enabled = true;
                txtPassword.Enabled = true;
                btnLogin.Enabled = true;
            }
            catch (Exception ex)
            {
                Util.logger.AppendLog(ex);
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            ntyIcon.Icon = this.Icon;
            if (Util.READY_TO_LOGIN)
            {
                txtAddress.Text = Util.SERVER_IP;
                txtUsername.Text = Util.USERNAME;
                txtPassword.Text = Util.PASSWORD;
                try
                {
                    DoLogin();
                }
                catch (Exception ex)
                {
                    Util.logger.AppendLog(ex);
                }
            }
        }

        private void ntyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void FrmMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.ShowInTaskbar = false;
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Util.IS_LOGIN)
            {
                LogoutResponseResult result;
                try
                {
                    result = Util.Logout(Util.LOGIN_URL, Util.UID);
                    if (result.Status == ResponseStatus.Error)
                    {
                        MessageBox.Show(result.Message);
                        Util.logger.AppendLog(result.ResponseText);
                    }
                }
                catch (Exception ex)
                {
                    Util.logger.AppendLog(ex);
                }
                finally
                {
                    Util.logger.Close();
                }
            }
        }

    }
}
